package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.rmi.dgc.Lease;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Autowired
    private  RoomInfoService roomInfoService;


    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private  FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Override
    public void customSaveOrUptate(ApartmentSubmitVo apartmentSubmitVo) {
        boolean falg = apartmentSubmitVo.getId() != null;

        //回显省份   service层的getByid 也可以
        Long provinceId = apartmentSubmitVo.getProvinceId();
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(provinceId);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        //回显城市
        Long cityId = apartmentSubmitVo.getCityId();
        CityInfo cityInfo = cityInfoMapper.selectById(cityId);
        apartmentSubmitVo.setCityName(cityInfo.getName());
        //回显区县
        Long districtId = apartmentSubmitVo.getDistrictId();
        DistrictInfo districtInfo = districtInfoMapper.selectById(districtId);
        apartmentSubmitVo.setDistrictName(districtInfo.getName());


        saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();
        if (falg) {

            //公寓标签
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //杂费中间表
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentId);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            //标签中间表

            LambdaQueryWrapper<ApartmentLabel> appartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            appartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelService.remove(appartmentLabelLambdaQueryWrapper);
            //图片表
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        //保存公寓配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> facilities = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();

                facilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilities);

        }

        //保存标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabels = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentId).build();
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //保存杂费值
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> apartmentfeevalues = new ArrayList<>(feeValueIds.size());
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build();
                apartmentfeevalues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentfeevalues);
        }

        //保存图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);

        }
    }

    @Override
    public IPage<ApartmentItemVo> customPage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.customPageSelect(page,queryVo);
        return page;
    }

    @Override
    public ApartmentDetailVo customGetById(Long id) {
        ApartmentInfo apartmentInfo = getById(id);

        //查询公寓id对应的公寓配套集合
        List<FacilityInfo> facilityInfoList= facilityInfoMapper.customSelectList(id);
        //查询公寓Id对应的标签集合
        List<LabelInfo> labelInfoList = labelInfoMapper.custonSelectList(id);
        //查询公寓id对应的杂费集合
        List<FeeValueVo> feeValueVoList = feeValueMapper.customSelectList(id);
        //查询公寓id对应的图片集合
        List<GraphVo> graphVoList = graphInfoMapper.customSelectList(ItemType.APARTMENT,id);
        //将结果合并到vo返回
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);

        //进行对象之间的相同属性赋值！！ 将参数1复制到参数2
        //参数1： 提供数据的对象 源对象
        //参数2： 接受数据的对象 目标对象
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        return apartmentDetailVo;
    }

    @Override
    public void customremoveById(Long id) {
        //先查询有没有房间 有房间提示207
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if(count > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //删除公寓信息
        removeById(id);

//删除四张关系表
        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //删除公寓-配套
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //删除公寓-标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        //删除公寓-杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
    }
}




